import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_pickers/more_pickers/init_data.dart';
import 'package:flutter_pickers/pickers.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'package:yi_shop/ui/draw_under_line.dart';
import 'package:yi_shop/utils/colors.dart';
import 'package:yi_shop/utils/styles.dart';
import 'package:yi_shop/utils/tools.dart';

class MineSettingInfoController extends GetxController {
  Rx<_UserModel> user = Rx(_UserModel(
    nickname: '原型大师',
    account: 'qxhlld321',
    avatar:
        'https://img0.baidu.com/it/u=3762686575,934747082&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1712768400&t=9ee37c534e5ae0adcc226f923ece69dd',
  ));
}

class MineSettingInfoPage extends GetView<MineSettingInfoController> {
  const MineSettingInfoPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: ToolsUtils.builderLeading(),
        title: Text(
          '用户信息',
          style: StylesUtils.customTextStyle(
              fontSize: StylesUtils.fontSize18, fontWeight: FontWeight.w700),
        ),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            DrawUnderLine(
              color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF2f2),
              child: Container(
                color: Colors.white,
                padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 15),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      '头像',
                      style: StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                    ),
                    Wrap(
                      crossAxisAlignment: WrapCrossAlignment.center,
                      alignment: WrapAlignment.end,
                      spacing: 10,
                      children: [
                        Obx(() => CircleAvatar(
                              radius: 25,
                              backgroundImage:
                                  CachedNetworkImageProvider(controller.user.value.avatar),
                            )),
                        InkWell(
                            onTap: () {
                              _showActionSheet(context);
                            },
                            child: Icon(
                              Icons.arrow_forward_ios,
                              size: 15,
                              color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorCccc),
                            )),
                      ],
                    )
                  ],
                ),
              ),
            ),
            DrawUnderLine(
              color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF2f2),
              child: Container(
                color: Colors.white,
                padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 15),
                constraints: const BoxConstraints(minHeight: 50),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      '用户名',
                      style: StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                    ),
                    Obx(() => Text(
                          controller.user.value.account,
                          style: StylesUtils.customTextStyle(color: ColorsUtils.color9999),
                        ))
                  ],
                ),
              ),
            ),
            DrawUnderLine(
              color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF2f2),
              child: Container(
                color: Colors.white,
                constraints: const BoxConstraints(minHeight: 50),
                padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 15),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      '昵称',
                      style: StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                    ),
                    Wrap(
                      spacing: 10,
                      crossAxisAlignment: WrapCrossAlignment.center,
                      alignment: WrapAlignment.end,
                      children: [
                        Obx(() => Text(
                              controller.user.value.nickname,
                              style: StylesUtils.customTextStyle(color: ColorsUtils.color9999),
                            )),
                        InkWell(
                            onTap: () {},
                            child: Icon(
                              Icons.arrow_forward_ios,
                              size: 15,
                              color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorCccc),
                            )),
                      ],
                    )
                  ],
                ),
              ),
            ),
            DrawUnderLine(
              color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorF2f2),
              child: Container(
                color: Colors.white,
                constraints: const BoxConstraints(minHeight: 50),
                padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 15),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      '性别',
                      style: StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                    ),
                    Wrap(
                      spacing: 10,
                      crossAxisAlignment: WrapCrossAlignment.center,
                      alignment: WrapAlignment.end,
                      children: [
                        Obx(() => Text(
                              controller.user.value.gender == 1 ? '男' : '女',
                              style: StylesUtils.customTextStyle(color: ColorsUtils.color9999),
                            )),
                        InkWell(
                            onTap: () {
                              Pickers.showSinglePicker(context,
                                  data: pickerData[PickerDataType.sex],
                                  selectData: '男',
                                  onConfirm: (dynamic data, int index) {});
                            },
                            child: Icon(
                              Icons.arrow_forward_ios,
                              size: 15,
                              color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorCccc),
                            )),
                      ],
                    )
                  ],
                ),
              ),
            ),
            Container(
              color: Colors.white,
              constraints: const BoxConstraints(minHeight: 50),
              padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 15),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    '出生日期',
                    style: StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                  ),
                  InkWell(
                      onTap: () {
                        _showDialog(
                            CupertinoDatePicker(
                              initialDateTime: DateTime.now(),
                              mode: CupertinoDatePickerMode.date,
                              use24hFormat: false,
                              showDayOfWeek: false,
                              itemExtent: 40,
                              dateOrder: DatePickerDateOrder.ymd,
                              onDateTimeChanged: (DateTime newDate) {},
                            ),
                            context);
                      },
                      child: Icon(
                        Icons.arrow_forward_ios,
                        size: 15,
                        color: ColorsUtils.parseColorFromHexString(ColorsUtils.colorCccc),
                      )),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  void _showDialog(Widget child, BuildContext context) {
    showCupertinoModalPopup<void>(
      context: context,
      builder: (BuildContext context) => Container(
        height: 250,
        margin: EdgeInsets.only(
          bottom: MediaQuery.of(context).viewInsets.bottom,
        ),
        color: CupertinoColors.systemBackground.resolveFrom(context),
        child: SafeArea(
          top: false,
          child: Column(
            children: [
              Container(
                height: 40,
                padding: const EdgeInsets.symmetric(horizontal: 15),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    TextButton(
                        onPressed: () {
                          Get.back();
                        },
                        child: Text(
                          '取消',
                          style: StylesUtils.customTextStyle(color: ColorsUtils.color6666),
                        )),
                    TextButton(
                        onPressed: () {
                          Get.back();
                        },
                        child: Text(
                          '确定',
                          style: StylesUtils.customTextStyle(color: ColorsUtils.color3333),
                        ))
                  ],
                ),
              ),
              Expanded(child: child)
            ],
          ),
        ),
      ),
    );
  }

  void _showActionSheet(BuildContext context) {
    ToolsUtils.photograph(context, callback: (XFile? file) {});
  }
}

class _UserModel {
  String nickname;

  String avatar;

  String account;

  int gender;

  _UserModel(
      {required this.nickname, required this.avatar, required this.account, this.gender = 1});
}
